﻿using ClassTableService.DTO;
using ClassTableService.Enum;
using ClassTableService.Helper;
using ClassTableService.Model;
using Microsoft.AspNetCore.Mvc;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Data;
using System.Data.Common;
using System.Diagnostics.Metrics;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ClassTableService.Controllers
{
    /// <summary>
    /// 实体类生成sql语句控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ClassToSqlController : ControllerBase
    {
        /// <summary>
        /// 根据实体类一键生成创建表的sql语句
        /// </summary>
        /// <returns></returns>
        [HttpGet("IClassToCreateSql")]
        public IActionResult IClassToCreateSql(string className, ServerTypeEnum serverType)
        {
            #region 操作示例
            //string sql = ClassServiceHelper.ClassToSql<Student>(Enum.ServerTypeEnum.MySql);
            //string sql1 = ClassServiceHelper.ClassToSql<Student>(Enum.ServerTypeEnum.SqlServer);
            //string sql2 = ClassServiceHelper.ClassToSql<Student>(Enum.ServerTypeEnum.Sqlite);
            #endregion

            string sql  = string.Empty;
            //暂时找不到把className转化成 T 参数的方法
            //只能用这种最笨的方式操作
            switch (className)
            {
                case "Student":
                    sql = ClassServiceHelper.ClassToSql<Student> (serverType);
                    break;
                case "Group":
                    sql = ClassServiceHelper.ClassToSql<Group>(serverType);
                    break;
                default:
                    break;
            }

            return Ok(serverType + ":" + sql);
        }

        /// <summary>
        /// 一键生成实体类
        /// </summary>
        /// <param name="data">参数字典:key-类名，value-属性、类型</param>
        /// <returns></returns>
        [HttpPost("ICreateClassFile")]
        public IActionResult ICreateClassFile([FromBody]Dictionary<string, List<GenericClassDTO>> data)
        {
            #region 请求示例
            //GenericClassDTO data1 = new GenericClassDTO() { PropertName = "Id", PropertRemark = "Id", PropertType = "string"};
            //GenericClassDTO data2 = new GenericClassDTO() { PropertName = "Name", PropertRemark = "名称", PropertType = "string"};
            //GenericClassDTO data3 = new GenericClassDTO() { PropertName = "Pwd", PropertRemark = "密码", PropertType = "string"};
            //GenericClassDTO data4 = new GenericClassDTO() { PropertName = "Age", PropertRemark = "年龄", PropertType = "int"};
            //List<GenericClassDTO> data = new List<GenericClassDTO>() { data1, data2, data3, data4 };
            //Dictionary<string, List<GenericClassDTO>> dic = new Dictionary<string, List<GenericClassDTO>>();
            //dic.Add("Users", data);
            #endregion

            int resultCode = ClassServiceHelper.CreateClassFie(data);
            string result = resultCode switch
            {
                -1 => "参数为空",
                -2 => "代码报错",
                200 => "操作成功",
                _ => "未知错误"
            };
            return Ok(result);
        }

        /// <summary>
        /// 一键生成实体类和sql语句
        /// </summary>
        /// <param name="data">参数字典:key-类名，value-属性、类型</param>
        /// <returns></returns>
        [HttpPost("ICreateClassFileAndSql")]
        public IActionResult ICreateClassFileAndSql([FromBody]CreateClassFileAndSqlDTO data)
        {
            int resultCode = ClassServiceHelper.CreateClassFieAndSql(data, data.ServerType, out string sql);
            string result = resultCode switch
            {
                -1 => "参数为空",
                -2 => "代码报错",
                200 => "操作成功",
                _ => "未知错误"
            };

            if (resultCode != 200)
                return Problem(result, null, resultCode);

            return Ok(sql);
        }
    }
}
